﻿namespace Ma.Bussiness.MaperExtend;

public static class ReflectionMapper
{
    /// <summary>
    /// 用反射 反射   进行对实体的转换
    /// </summary>
    /// <typeparam name="Tin"></typeparam>
    /// <typeparam name="Tout"></typeparam>
    /// <param name="tin"></param>
    /// <returns></returns>
    public static Tout Maper<Tin, Tout>(Tin tin)
    {
        Tout result = Activator.CreateInstance< Tout>();
        foreach ( var item in result.GetType().GetProperties() )
        {
            var props= tin.GetType().GetProperty(item.Name);
            if ( props != null )
            item.SetValue( result, props.GetValue(tin), null );
        }
        foreach (var item in result.GetType().GetFields())
        {
            var props =tin.GetType().GetField(item.Name);
            if (props!=null)
                item.SetValue(result,props.GetValue(tin));
        }
        return result;

    }
}
